<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of housekeepingAction
 *
 * @author xlp
 */
class housekeepingAction extends commonAction {

    private $cacheFilePre = 'house/visit_vid_'; //保存缓存信息的文件名前缀
    private $info = array('否', '<span class="red">是</span>');
    private $status = array('<span class="red">屏蔽</span>', '正常');
    private $type = array('求', '荐');
    private $topicScreen = array('f1' => '月嫂', 'f2' => '育儿', 'f3' => '钟点工', 'f4' => '园林工', 'f5' => '涉外', 'f6' => '保姆');

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    function index() {
        $title = $this->_get('q');
        $stime = $this->_get('stime');
        $etime = $this->_get('etime');
        $p = $this->_getid('p', 1);
        $uid = $this->_getid('uid', 0);
        $village_id = $this->_getid('village_id', 0);
        $type = $this->_getid('type', 0);
        $hot = $this->_getid('hot', 0);
        $top = $this->_getid('top', 0);
        $report = $this->_getid('report', 0);
        $status = $this->_getid('status', 0);
        $recycle = false; //是否回收站
        $where = array();
        if ($title) {
            $where['LIKE'] = array('title' => parent::safeSearch($title));
        }
        if ($stime && $etime) {
            $stime = inTime($stime);
            $etime = inTime($etime) + (24 * 60 * 60) - 1;
        }
        if ($stime && $etime) {
            $where['infotime[>=]'] = $stime;
            $where['infotime[<=]'] = $etime;
        }
        if ($uid) {
            $where = array('uid' => $uid);
        }
        if ($village_id) {
            $where = array('village_id' => $village_id);
        }
        if ($type) {
            $where = array('type' => ($type - 1));
        }
        if ($hot) {
            $where = array('is_hot' => $hot);
        }
        if ($top) {
            $where = array('is_top' => $top);
        }
        if ($report) {
            $where = array('is_report' => $report);
        }
        if ($status) {
            $where = array('status' => 0);
            $recycle = true;
        } else {
            $where['status'] = 1;
        }
        $rs = M('housekeeping')->where($where)->page($p)->order('hid DESC')->findAll();
        $total = M('housekeeping')->getTotal();
//        z(M('area')->getAllSql());
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['user_name'] = getUser($v['uid']);
                $rs[$k]['village_name'] = parent::getVillageName($v['village_id']);
            }
        }
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'info' => $this->info, 'status' => $this->status, 'type' => $this->type, 'recycle' => $recycle));
//        print_r($this->getAllVar());
        $this->display();
    }

    function detail() {
        $hid = $this->_getid('hid');
        $p = $this->_getid('p', 1);
        if (!$hid) {
            showError('参数丢失');
        }
        $rs = M('housekeeping')->where(array('hid' => $hid))->find();
        if (!$rs) {
            showError('主题不存在或者已被删除');
        }
        $rs['content'] = express($rs['content']);
        $rs['tags'] && $rs['tags'] = explode(',', trim($rs['tags'], ','));
        if ($rs['tags']) {
            $tags = array();
            foreach ($rs['tags'] as $v) {
                $tags[] = $this->topicScreen[$v];
            }
            $rs['tags'] = $tags;
        }
        $rs['lbs_info'] = json_decode($rs['lbs_info'], true);
        $rs['pics_list'] = $rs['photo_counts'] ? D('album')->getPhotosById($rs['uid'], $rs['pics_list']) : array();
        $this->assign(array('rs' => $rs));
        $this->display();
    }

    function setting() {
        $id = $this->_postid('id');
        $isHot = $this->_postid('is_hot', 0);
        $isTop = $this->_postid('is_top', 0);
        $isOk = $this->_postid('is_ok', 0);
        $where = array();
        if (!$id) {
            showError('参数丢失');
        }
        if ($isHot) {
            $where = $isHot == 1 ? array('is_hot' => 1) : array('is_hot' => 0);
        }
        if ($isTop) {
            $where = $isTop == 1 ? array('is_top' => 1) : array('is_top' => 0);
        }
        if ($isOk) {
            $where = array('status' => 1);
        }
        if ($where) {
            M('housekeeping')->update($where, array('hid' => $id));
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('error');
        }
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $housekeeping = M('housekeeping')->field('uid,village_id')->where(array('hid' => $id))->find();
        if (!$housekeeping) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('housekeeping')->update(array('status' => 0), array('hid' => $id));
        F($this->cacheFilePre . $housekeeping['village_id'], null); //清除统计缓存
        $this->JsonReturn('ok', null, 1);
    }

    function clear() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $housekeeping = M('housekeeping')->field('uid,village_id')->where(array('hid' => $id))->find();
        if (!$housekeeping) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('housekeeping')->delete(array('hid' => $id));
//        //删除所有回帖
        M('housekeeping_post')->delete(array('hid' => $id));
        F($this->cacheFilePre . $housekeeping['village_id'], null); //清除统计缓存
        $this->JsonReturn('ok', null, 1);
    }

}
